<?php
	namespace app\index\controller;
	use think\Controller;
	use app\index\model\person;
	use app\index\model\usermsg;
	use app\index\model\adminmsg;
	use app\index\model\shopping;
	use app\index\validate\Person as PersonUser;
	use app\index\controller\Base;
	use think\Image;
	use think\Request;
	class Index extends Base{
		protected $is_check_login=['index','submission','submissionmsg'];
		public function remove(){
			session(null);
			$this->success('退出登陆成功','Index/login');
		}
		public function index(){
			$data=shopping::all();
			$this->assign('data',$data); 
			return $this->fetch();
		}
		public function login(){
			return $this->fetch();
		}
		public function register(){
			return $this->fetch();
		}
		public function info(){
			$val1=adminmsg::all(['type'=>1]);
			$val2=adminmsg::all(['type'=>2]);
			$this->assign('val',$val1);
			$this->assign('vals',$val2);
			return $this->fetch();
		}
		public function add(){
			$data=input('post.');
			$val=new PersonUser();
			if(!$val->check($data)){
				$this->error($val->getError());
				exit();
			};
			if(!captcha_check($data['code'])){
				$this->error('验证码错误');
			}
			$user=new person();
			$ret=$user->where('name',$data['name'])->find();
			if(!$ret){
				$user->data([
					'name'  =>  $data['name'],
					'password' =>  $data['password']
				]);
				if($user->allowField(true)->save()){
					$this->success('新增用户成功','Index/index');
				}else{
					$this->error('新增用户失败');
				}
			}else{
				$this->error('用户名已经存在');
			}
		}
		//登录
		public function check(){
			$data=input('post.');
			$user=new person();
			if(!captcha_check($data['code'])){
				$this->error('验证码错误');
			}
			$where=array(
				"name"=>$data['name'],
				"password"=>$data['password']
			  );
			$res=$user->where($where)->find();
			if($res){
				session('name',$data['name']);
				$this->success('登录成功','Index/info');
			}else{
				$this->error('用户名和密码不匹配');
			}
		}
		public function submission(){
			$usermsg=new usermsg();
			$where=array(
				"name"=>session('name'),
				"type"=>1
			);
			$res=$usermsg->where($where)->find();
			if($res){
				$this->assign('flog','tg');
			}else{
				$wheres=array(
					"name"=>session('name'),
					"type"=>0
				);
				$val=$usermsg->where($wheres)->find();
				if($val){
					$this->assign('flog','cz');
				}else{
					$this->assign('flog','pass');
				}
			}
			return $this->fetch();
		}
		public function submissionmsg(){
			$data=input('post.');
			$file = request()->file('logoUrl');
			$file1 = request()->file('logoUrl1');
			$result=$this->validate(['file1'=>$file],['file1'=>'require'],['file1.require'=>'请上传格式为100*100的PNG格式文件']);
			$result1=$this->validate(['file1'=>$file1],['file1'=>'require'],['file1.require'=>'请上传格式为100*100的PNG格式文件']);
			if (true !== $result) 
      		{
				$this->error($result['file1']);
			}
			if (true !== $result1) 
      		{
				$this->error($result['file1']);
			}

			$info = $file->rule(function($file){
				return session('name')."".$file->getInfo('type').date('--m-d_H-i-s');
			})->move(ROOT_PATH.'public\uploads\image');

			$info1 = $file1->rule(function($file1){
				return session('name')."".$file1->getInfo('type').date('--m-d_H-i-s-1');
			})->move(ROOT_PATH.'public\uploads\image');

			if($info&&$info1){
					$user=new usermsg();
					$user->data([
						'name'  => session('name'),
						'photo' => $data['photo'],
						'email' => $data['email'],
						'username' => $data['username'],
						'cardnumber' => $data['cardnumber'],
						'city' => $data['city'],
						'sex' => $data['sex'],
						'desc'=>$data['desc'],
						'url'=>'/uploads/image/'.$info->getSaveName(),
						'url1'=>'/uploads/image/'.$info1->getSaveName()
					 ]);
				if($user->allowField(true)->save()){
					return "<script>alert('添加成功');window.location.href='info?cancel=yes';</script>";
				}
			}else{
				$this->error("上传出错");
			}
		}
		public function newinfo($id){
			$msg=new adminmsg();
			$msg->where(['id'=>$id])->setInc('frequency');
			$data=adminmsg::get($id);
			$this->assign('data',$data);
			return $this->fetch();
		}
		public function shoppinfo($id){
			$data=shopping::get($id);
			return json($data);
		}
		// public function shwww(){
		// 	dump(ROOT_PATH);
		// }
	}
?>